<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Services\Form\initFormData as InitFormData;
use Illuminate\Http\Request;

class TanwinLib extends Controller
{
    use \App\Traits\CacheTrait;
    public $request;
    public $formData;
    public $breadMenuList;
    use \App\Traits\ApiTrait;
    public $curl;

    public function __construct(Request $request, InitFormData $formData)
    {
        $this->request = $request;
        $this->formData = $formData;
        $this->breadMenuList = config('front.bread_menu');
        $this->curl = new \Ixudra\Curl\CurlService();
    }

    /**
     * 碳银研究院列表
     * @return
     */
    public function index()
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::MENU_TANWIN_LIB);
        $result = $this->formData->getFilterTanwinLib($this->request, $currentTabMenuList, parent::CODE_ARTICLE_LIST);
        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;
        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'pageCount' => $pageCount,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
                'ajax' => true,
            ];
            if ($result['filterType'] == 1) {
                return view('web.tanwin_lib.list.image_title_subtitle_desc', $viewData);
            } elseif ($result['filterType'] == 2) {
                return view('web.tanwin_lib.list.image_title_param_desc', $viewData);
            } elseif ((int) $result['filterType'] == 3) {
                return view('web.tanwin_lib.list.type_3', $viewData);
            }
        } else {
            $otherDicList = $this->cacheGetOtherDicList();
            $viewData = [
                'seo' => $this->returnSeoInfo(parent::MENU_TANWIN_LIB, $currentTabMenuList[$result['filterType']]['name']),
                'selectedMenu' => parent::MENU_TANWIN_LIB,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_TANWIN_LIB],

                'currentTabMenuList' => $currentTabMenuList,
                'tabMenuMsg' => $currentTabMenuList[$result['filterType']]['msg'],

                'dataList' => $dataList,

                'filter_area' => parent::MENU_TANWIN_LIB,
                'filter_type' => $result['filterType'],

                'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
                'pageCount' => $pageCount,
                'domainList' => $otherDicList['LYZD'],
            ];
            return view('web.tanwin_lib.index', $viewData);
        }
    }
    /*V31研究院列表
    */
    public function indexInstitute(Request $request)
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::MENU_TANWIN_LIB);
        $result = $this->formData->getFilterTanwinLib($this->request, $currentTabMenuList, parent::CODE_ARTICLE_LIST);
        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;
        $pageNumber=$request->page;
        $result['filterType']=$this->request->type;
        if($result['filterType']==0)$result['filterType']=1;
        if($pageNumber==0)$pageNumber=1;
        $pagesize=10;
        $idArr=[
          '2'=>'25/26',
          '4'=>'25/27',
          '1'=>'25/28',
          '3'=>'25/29',
        ];
        // print_r($result['filterType']);
        if ($this->request->method() == 'POST') {
            $viewData = [
                'seo' => $this->returnSeoInfo('tanwin_lib'),
                'selectedMenu' => parent::MENU_TANWIN_LIB,
                'dataList' => $dataList,
                'pageCount' => $pageCount,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
                'ajax' => true,
            ];
            if ($result['filterType'] == 1) {
                $pagesize=9;
                $strR=strval($result['filterType']);
                $categoryId=$idArr[$strR];
                // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
                $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
                // var_dump($list);
                if($list['code']!=-1){
                  $listData=$list['data']['records'];
                  // print_r($listData);
                }
                else $listData=[];
                $viewData['dataList']=$listData;
                $viewData['pageNumber']=$pageNumber;
                $viewData['count']=$list['data']['total'];
                $viewData['pagesize']=9;
                return view('web.tanwin_lib.research', $viewData);
            } elseif ($result['filterType'] == 2) {
                $pagesize=9;
                $strR=strval($result['filterType']);
                $categoryId=$idArr[$strR];
                // print_r($categoryId);
                // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
                $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
                // var_dump($list);
                if($list['code']!=-1){
                  $listData=$list['data']['records'];
                  // print_r($listData);
                }
                else $listData=[];
                $viewData['dataList']=$listData;
                $viewData['pageNumber']=$pageNumber;
                $viewData['count']=$list['data']['total'];
                $viewData['pagesize']=9;
                return view('web.tanwin_lib.read', $viewData);
            } elseif ((int) $result['filterType'] == 3) {
                return view('web.tanwin_lib.professor', $viewData);
            } elseif ((int) $result['filterType'] == 4) {
                return view('web.tanwin_lib.picture', $viewData);
            }
        } else {
          $viewData = [
              'seo' => $this->returnSeoInfo('tanwin_lib'),
              'selectedMenu' => parent::MENU_TANWIN_LIB,
              'dataList' => $dataList,
              'pageCount' => $pageCount,
              'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
              'ajax' => true,
          ];
          if ($result['filterType'] == 1) {
            $pagesize=9;
            $strR=strval($result['filterType']);
            $categoryId=$idArr[$strR];
            // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
            $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
            // var_dump($list);
            if($list['code']!=-1){
              $listData=$list['data']['records'];
              // print_r($listData);
            }
            else $listData=[];
            $viewData['dataList']=$listData;
            $viewData['pageNumber']=$pageNumber;
            $viewData['count']=$list['data']['total'];
            if($viewData['count']%9!=0)
            $maxPage=intval($viewData['count']/9)+1;
            else $maxPage=intval($viewData['count']/9);
            $viewData['maxPage']=$maxPage;
            // $viewData['count']=100;
            $viewData['pagesize']=9;
            return view('web.tanwin_lib.research', $viewData);
          } elseif ($result['filterType'] == 2) {
            $pagesize=9;
            $strR=strval($result['filterType']);
            $categoryId=$idArr[$strR];
            // print_r($categoryId);
            // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
            $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
            // var_dump($list);
            if($list['code']!=-1){
              $listData=$list['data']['records'];
              // print_r($listData);
            }
            else $listData=[];
            $viewData['dataList']=$listData;
            $viewData['pageNumber']=$pageNumber;
            $viewData['count']=$list['data']['total'];
            $viewData['pagesize']=9;
            if($viewData['count']%9!=0)
            $maxPage=intval($viewData['count']/9)+1;
            else $maxPage=intval($viewData['count']/9);
            $viewData['maxPage']=$maxPage;
            return view('web.tanwin_lib.read', $viewData);
          } elseif ((int) $result['filterType'] == 3) {
              return view('web.tanwin_lib.professor', $viewData);
          } elseif ((int) $result['filterType'] == 4) {
              return view('web.tanwin_lib.picture', $viewData);
          }
            die;
            $otherDicList = $this->cacheGetOtherDicList();
            $viewData = [
                'seo' => $this->returnSeoInfo(parent::MENU_TANWIN_LIB, $currentTabMenuList[$result['filterType']]['name']),
                'selectedMenu' => parent::MENU_TANWIN_LIB,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_TANWIN_LIB],

                'currentTabMenuList' => $currentTabMenuList,
                'tabMenuMsg' => $currentTabMenuList[$result['filterType']]['msg'],

                'dataList' => $dataList,

                'filter_area' => parent::MENU_TANWIN_LIB,
                'filter_type' => $result['filterType'],

                'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
                'pageCount' => $pageCount,
                'domainList' => $otherDicList['LYZD'],
            ];
            return view('web.tanwin_lib.index', $viewData);

        }
    }

    public function indexMobile(Request $request){
      $currentTabMenuList = config('front.tab_menu.' . parent::MENU_TANWIN_LIB);
      $result = $this->formData->getFilterTanwinLib($this->request, $currentTabMenuList, parent::CODE_ARTICLE_LIST);
      $result['filterType']=$this->request->type;
      if($result['filterType']==0)$result['filterType']=1;
      // $dataList = $result ? $result['dataList'] : [];
      // $pageCount = $dataList ? $result['pageCount'] : 1;
      $pageNumber=$request->page;
      if($pageNumber==0)$pageNumber=1;
      $pagesize=100;
      $idArr=[
        '2'=>'25/26',
        '4'=>'25/27',
        '1'=>'25/28',
        '3'=>'25/29',
      ];
      // print_r($result['filterType']);
      if ($this->request->method() == 'POST') {
          $viewData = [
              'seo' => $this->returnSeoInfo('m_lib'),
              'selectedMenu' => parent::MENU_TANWIN_LIB,
              'dataList' => $dataList,
              'pageCount' => $pageCount,
              'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
              'ajax' => true,
          ];
          if ($result['filterType'] == 1) {
              $pagesize=100;
              $strR=strval($result['filterType']);
              $categoryId=$idArr[$strR];
              // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
              $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
              // var_dump($list);
              if($list['code']!=-1){
                $listData=$list['data']['records'];
                // print_r($listData);
              }
              else $listData=[];
              $viewData['dataList']=$listData;
              $viewData['pageNumber']=$pageNumber;
              $viewData['count']=$list['data']['total'];
              $viewData['pagesize']=9;
              return view('mobile.tanwin_lib.research', $viewData);
          } elseif ($result['filterType'] == 2) {
              $pagesize=100;
              $strR=strval($result['filterType']);
              $categoryId=$idArr[$strR];
              // print_r($categoryId);
              // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
              $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
              // var_dump($list);
              if($list['code']!=-1){
                $listData=$list['data']['records'];
                // print_r($listData);
              }
              else $listData=[];
              $viewData['dataList']=$listData;
              $viewData['pageNumber']=$pageNumber;
              $viewData['count']=$list['data']['total'];
              $viewData['pagesize']=9;
              return view('mobile.tanwin_lib.read', $viewData);
          } elseif ((int) $result['filterType'] == 3) {
              return view('mobile.tanwin_lib.professor', $viewData);
          } elseif ((int) $result['filterType'] == 4) {
              return view('mobile.tanwin_lib.picture', $viewData);
          }
      } else {
        $viewData = [
            'seo' => $this->returnSeoInfo('m_lib'),
            'selectedMenu' => parent::MENU_TANWIN_LIB,
            'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
            'ajax' => true,
        ];
        if ($result['filterType'] == 1) {
          $pagesize=100;
          $strR=strval($result['filterType']);
          $categoryId=$idArr[$strR];
          // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
          $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
          // var_dump($list);
          if($list['code']!=-1){
            $listData=$list['data']['records'];
            // print_r($listData);
          }
          else $listData=[];
          $viewData['dataList']=$listData;
          $viewData['pageNumber']=$pageNumber;
          $viewData['count']=$list['data']['total'];
          if($viewData['count']%9!=0)
          $maxPage=intval($viewData['count']/9)+1;
          else $maxPage=intval($viewData['count']/9);
          $viewData['maxPage']=$maxPage;
          // $viewData['count']=100;
          $viewData['pagesize']=100;
          return view('mobile.tanwin_lib.research', $viewData);
        } elseif ($result['filterType'] == 2) {
          $pagesize=9;
          $strR=strval($result['filterType']);
          $categoryId=$idArr[$strR];
          // print_r($categoryId);
          // print_r($categoryId.'-'.$pageNumber.'-'.$pagesize);
          $list=$this->ajaxArticleList($categoryId,$pageNumber,$pagesize);
          // var_dump($list);
          if($list['code']!=-1){
            $listData=$list['data']['records'];
            // print_r($listData);
          }
          else $listData=[];
          $viewData['dataList']=$listData;
          $viewData['pageNumber']=$pageNumber;
          $viewData['count']=$list['data']['total'];
          $viewData['pagesize']=100;
          if($viewData['count']%9!=0)
          $maxPage=intval($viewData['count']/9)+1;
          else $maxPage=intval($viewData['count']/9);
          $viewData['maxPage']=$maxPage;
          return view('mobile.tanwin_lib.read', $viewData);
        } elseif ((int) $result['filterType'] == 3) {
            return view('mobile.tanwin_lib.professor', $viewData);
        } elseif ((int) $result['filterType'] == 4) {
            return view('mobile.tanwin_lib.picture', $viewData);
        }
          $otherDicList = $this->cacheGetOtherDicList();
          $viewData = [
              'seo' => $this->returnSeoInfo('m_lib'),
              'selectedMenu' => parent::MENU_TANWIN_LIB,

              'breadMenuList' => $this->breadMenuList,
              'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_TANWIN_LIB],

              'currentTabMenuList' => $currentTabMenuList,
              'tabMenuMsg' => $currentTabMenuList[$result['filterType']]['msg'],

              'dataList' => $dataList,

              'filter_area' => parent::MENU_TANWIN_LIB,
              'filter_type' => $result['filterType'],

              'infoUrl' => $this->returnInfoUrl(parent::MENU_TANWIN_LIB, ['type' => parent::MENU_TANWIN_LIB, 'flag' => $result['filterType']]),
              'pageCount' => $pageCount,
              'domainList' => $otherDicList['LYZD'],
          ];
          return view('mobile.tanwin_lib.read', $viewData);
      }
    }

    public function ajaxArticleList($id,$pageNumber=1,$pageSize=10){
      $categoryId=$id;
      // $pageNumber=$this->msgPageNum;
      //验证是否有cookie信息
        $rqUrl='http://api.crm.tanwin.cn/article/findByProgramaId';
        $rqData=['article_programa_id'=>$categoryId,'pageNumber'=>$pageNumber,'pageSize'=>$pageSize];
        $result=$this->curl->to($rqUrl)
        ->withOption('TIMEOUT', 10)
        ->withData($rqData)
        ->post();
        // print_r('请求地址是'.$rqUrl.'</br>');
        // print_r('请求结果是'.$result);
        try {
            $result=json_decode($result,true);
            if($result['code']==100){
              $rData=$result['data'];
              $data['code']=1;
              $data['data']=$rData;
              return $data;
            }
          } catch (Exception $e) {
            $data['data']=null;
            $data['code']=-1;
            return $data;
          }
        }
}
